<template>
  <div class="change-avatar">
    <!-- 使用用户名缩写头像 -->
    <div class="avatar-wrapper">
      <div class="username-avatar">
        {{ getUsernameInitials() }}
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { propTypes } from '@/utils/propTypes'
import { useUserStore } from '@/store/modules/user'
import { getUserProfile } from '@/api/system/user/profile'

defineOptions({ name: 'UserAvatar' })

defineProps({
  img: propTypes.string.def('')
})

const userStore = useUserStore()
const username = ref('')

// 获取用户信息
const getUserInfo = async () => {
  try {
    const userInfo = await getUserProfile()
    username.value = userInfo.username || userInfo.nickname || 'User'
  } catch (error) {
    username.value = 'User'
  }
}

// 获取用户名缩写
const getUsernameInitials = () => {
  if (!username.value) return 'U'
  const names = username.value.split(' ')
  if (names.length >= 2) {
    return (names[0][0] + names[1][0]).toUpperCase()
  }
  return username.value.substring(0, 2).toUpperCase()
}

onMounted(() => {
  getUserInfo()
})
</script>

<style lang="scss" scoped>
.change-avatar {
  display: flex;
  justify-content: center;
  align-items: center;
  
  .avatar-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  .username-avatar {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 36px;
    font-weight: bold;
    color: white;
    text-transform: uppercase;
    letter-spacing: 2px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    transition: all 0.3s ease;
    text-align: center;
    line-height: 1;
    vertical-align: middle;
    
    &:hover {
      transform: scale(1.05);
      box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
    }
  }
}
</style>
